package com.clientmanage.controller;

import com.clientmanage.domain.vo.clientCareReminderVo;
import com.clientmanage.domain.resp.PageResult;
import com.clientmanage.domain.resp.Result;
import com.clientmanage.service.ICustomerCareReminderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 客户关怀控制器
 * 实现客户关怀与提醒相关接口
 */
@RestController
@RequestMapping("/customer/care")
public class CustomerCareReminderController {
    @Autowired
    private ICustomerCareReminderService customerCareReminderService;

    /**
     * 获取关怀提醒列表
     * 接口地址：GET /customer/care/list
     */
    @GetMapping("/list")
    public Result<PageResult<clientCareReminderVo>> getCareReminderList(
        @RequestParam(required = false) String customerName,
        @RequestParam(required = false) String petName,
        @RequestParam(required = false) String careType,
        @RequestParam(required = false) String status,
        @RequestParam(defaultValue = "1") int pageNum,
        @RequestParam(defaultValue = "10") int pageSize
    ) {
        return Result.success(customerCareReminderService.pageQuery(customerName, petName, careType, status, pageNum, pageSize));
    }

    /**
     * 新增关怀提醒
     * 接口地址：POST /customer/care/add
     * 参数：customerId, petId, careType, triggerDate, advanceDays, sendMethod, template
     * 说明：前端需传客户ID和宠物ID，后端校验存在性
     */
    @PostMapping("/add")
    public Result<Void> addCareReminder(@RequestBody clientCareReminderVo vo) {
        try {
            customerCareReminderService.saveOrUpdateCare(vo);
            return Result.success();
        } catch (IllegalArgumentException e) {
            return Result.fail(e.getMessage(), 400);
        }
    }

    /**
     * 更新关怀提醒
     * 接口地址：PUT /customer/care/update
     */
    @PutMapping("/update")
    public Result<Void> updateCareReminder(@RequestBody clientCareReminderVo vo) {
        customerCareReminderService.saveOrUpdateCare(vo);
        return Result.success();
    }

    /**
     * 删除关怀提醒
     * 接口地址：DELETE /customer/care/delete/{id}
     */
    @DeleteMapping("/delete/{id}")
    public Result<Void> deleteCareReminder(@PathVariable String id) {
        customerCareReminderService.deleteById(id);
        return Result.success();
    }

    /**
     * 立即发送关怀
     * 接口地址：POST /customer/care/send/{id}
     */
    @PostMapping("/send/{id}")
    public Result<Void> sendCareReminder(@PathVariable String id) {
        customerCareReminderService.sendCareReminder(id);
        return Result.success();
    }

    /**
     * 批量生成关怀提醒
     * 接口地址：POST /customer/care/batch-generate
     */
    @PostMapping("/batch-generate")
    public Result<Void> batchGenerateCareReminder(@RequestBody clientCareReminderVo vo) {
        customerCareReminderService.batchGenerateCareReminder(vo);
        return Result.success();
    }
}
